package com.water.fish

import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 *  鱼的基类,框定类型.
 *  Created by zxn on 2021/3/30.
 **/
sealed class FishEntity {

    var moveSpeed = 100L

    /**
     * 当前鱼的展示状态,初始值向左
     */
    open var skinResId = 0

}

/**
 * 鱼宠
 */
class PetFish : FishEntity() {

    var recoverDelayMillis = 3000L

    /**
     * 鱼的状态
     */
    var fishStatus = FishStatus.NORMAL
        private set

    /**
     * 鱼的上一个状态
     */
    var lastStatus = FishStatus.NORMAL
        private set

    var tipsList = mutableListOf<String>()
        set(value) {
            field = value
            tipIndex = 0
        }

    private val mStringBuilder by lazy {
        StringBuilder()
    }

    var moveLeftResId: Int = 0

    /**
     * 喷水左移
     */
    var spurtLeftResId: Int = 0

    var thirstyLeftResId: Int = 0

    /**
     * 兴奋左移
     */
    var gladLeftResId: Int = 0

    /**
     * 增肥左移
     */
    var fatLeftResId: Int = 0

    /**
     * 变美左移
     */
    var prettyLeftResId: Int = 0

    var turnRightResId: Int = 0

    var thirstyTurnRightResId: Int = 0

    /**
     * 使用正则表达式来判断字符串中是否包含字母
     * @param str 待检验的字符串
     * @return 返回是否包含
     * true: 包含字母 ;false 不包含字母
     */
    fun containsLetters(str: String): Boolean {
        val regex = ".*[a-zA-Z]+.*"
        val m: Matcher = Pattern.compile(regex).matcher(str)
        return m.matches()
    }

    private fun isChinese(c: Char): Boolean {
        val ub = Character.UnicodeBlock.of(c)
        return (ub === Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub === Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub === Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub === Character.UnicodeBlock.GENERAL_PUNCTUATION // GENERAL_PUNCTUATION 判断中文的“号
                || ub === Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION // CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
                || ub === Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
    }

    /**
     * 判断是否包含中午,包含中文则为中文
     */
    private fun isChinese(str: String): Boolean {
        val ch = str.toCharArray()
        for (c in ch) {
            if (isChinese(c)) return true
        }
        return false
    }

    /**
     * 当前tips位置索引.
     */
    var tipIndex = 0

    fun tips(): String? {
        if (tipsList.isEmpty()) {
            tipIndex = 0
            return null
        }
        tipsList[tipIndex].also {
            mStringBuilder.clear()
            tipIndex++
            if (tipIndex == tipsList.size) {
                tipIndex = 0
            }

            if (isChinese(it)) {
                return when (it.length) {
                    in 0..7 -> it
                    in 8..14 -> mStringBuilder.append(it.substring(0, 7))
                        .append("\n")
                        .append(it.substring(7)).toString()
                    else -> mStringBuilder.append(it.substring(0, 7))
                        .append("\n")
                        .append(it.substring(7, 13))
                        .append("...")
                        .toString()
                }
            } else {
                val wordList = it.split(" ")
                return when (wordList.size) {
                    in 0..3 -> it
                    in 4..6 -> {
                        wordList.forEachIndexed { index, word ->
                            if (index == 2) {
                                mStringBuilder.append("$word\n")
                            } else {
                                mStringBuilder.append("$word ")
                            }
                        }
                        mStringBuilder.toString()
                    }
                    else -> {
                        wordList.forEachIndexed { index, word ->
                            if (index <= 5) {
                                if (index == 2) {
                                    mStringBuilder.append("$word\n")
                                } else {
                                    mStringBuilder.append("$word ")
                                }
                            }
                        }
                        mStringBuilder.toString()
                    }
                }
            }
        }
    }

    fun updateFishStatus(@FishStatus status: Int) {
        this.lastStatus = fishStatus
        this.fishStatus = status
    }

    fun onMoveResId(): Int = when (fishStatus) {
        FishStatus.BEAUTIFY -> prettyLeftResId
        FishStatus.FLESH_UP -> fatLeftResId
        FishStatus.EXCITING -> gladLeftResId
        FishStatus.THIRSTY -> thirstyLeftResId
        FishStatus.SPRAY -> spurtLeftResId
        FishStatus.NORMAL -> moveLeftResId
        else -> moveLeftResId
    }

    fun onTurnResId(): Int = when (fishStatus) {
        FishStatus.THIRSTY -> thirstyTurnRightResId
        FishStatus.SPRAY,FishStatus.NORMAL -> turnRightResId
        else -> turnRightResId
    }

    fun resetAllResId(){
        prettyLeftResId = 0
        fatLeftResId = 0
        gladLeftResId = 0
        thirstyLeftResId = 0
        spurtLeftResId = 0
        moveLeftResId = 0

        thirstyTurnRightResId = 0
        turnRightResId = 0
    }

}

/**
 * 鱼群,ShoalFish
 */
class ShoalFish : FishEntity() {

    var skinResList = mutableListOf<Int>()

    fun shuffledSkinResId() = if (skinResList.isEmpty()) 0 else skinResList.shuffled().take(1)[0]

}

///**
// * 贝壳
// */
//class Shell : FishEntity() {
//    /**
//     * 石头ID
//     */
//    var stoneResId: Int = 0
//
//    /**
//     * 贝壳
//     */
//    var shellResId: Int = 0
//
//}



